<?php
/**
 *	商户拜访日志
 *	销售权限
 *	商户拜访日志新增------>session未添加
 */
namespace Admin\Controller\Facilitator;
use Think\Controller;

class VisitlogController extends Controller 
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
	public function _initialize() 
	{
		session('regist_id','232');
		// 判断用户是否登陆
		if (!session('?regist_id')) //	!session('?regist_id')#####1!=1
		{
			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
			redirect($url);
			// echo "<script>alert('权限不足，无法操作');window.location.href='".$url."';</script>";
			exit;
		}
	}


	/**
	 *	商户拜访日志列表管理页
	 */
	public function visitlog_manage() 
	{
		$select_name = I("post.select_name","");
		$select_businesstype = I("post.select_businesstype","");
		$select_circle = I("post.select_circle","");
		$select_time = I("post.select_time","");
		$select_user = I("post.select_user","");
		$select_mark = I("post.select_mark","");

		$search_array['select_name'] = $select_name;
		$search_array['select_businesstype'] = $select_businesstype;
		$search_array['select_circle'] = $select_circle;
		$search_array['select_time'] = $select_time;
		$search_array['select_user'] = $select_user;
		$search_array['select_mark'] = $select_mark;

		$this->assign("search_array",$search_array);

		$model_visitlog = D("bypal_facilitator_visitlog");

		if ($select_name != '') 
		{
			$condition['visitlog_name'] = array('like','%'.$select_name.'%');
		}
		if ($select_businesstype != '') 
		{
			$condition['businesstype_id'] = $select_businesstype;
		}
		if ($select_circle != '') 
		{
			$condition['circle_id'] = $select_circle;
		}
		if ($select_time != '') 
		{
			$condition['create_time'] = array(array('egt',strtotime($select_time)),array('elt',strtotime($select_time)+86400));
		}
		if ($select_user != '') 
		{
			$condition['user_id'] = $select_user;
		}
		if ($select_mark != '') 
		{
			$condition['visitlog_mark'] = $select_mark;
		}

		// $condition['visitlog_mark'] = array('neq','2');
		$array_visitlog = $model_visitlog->where($condition)->order('create_time desc')->select();
		foreach ($array_visitlog as $key => $value) 
		{
			if ($value['visitlog_type'] == '1') 
			{
				$array_visitlog[$key]['visitlog_type'] = '主动拜访';
			}
			elseif ($value['visitlog_type'] == '2') 
			{
				$array_visitlog[$key]['visitlog_type'] = '商户联系';
			}

			$model_visitlogdetail = D("bypal_facilitator_visitlogdetail");
			$count = $model_visitlogdetail->where('visitlog_id='.$value['visitlog_id'])->count();
			$array_visitlog[$key]['visitlogdetail_count'] = $count;

			$array_visitlog[$key]['create_time'] = date('Y-m-d', $value['create_time']);
			$array_visitlog[$key]['update_time'] = date('Y-m-d', $value['update_time']);

			if ($value['visitlog_mark'] == '0') 
			{
				$array_visitlog[$key]['mark'] = '淘汰';
			}
			elseif ($value['visitlog_mark'] == '1') 
			{
				$array_visitlog[$key]['mark'] = '跟进';
			}
			elseif ($value['visitlog_mark'] == '2') 
			{
				$array_visitlog[$key]['mark'] = '已转审核';
			}

			//	商务代表姓名
			$model_userName = D("bypal_comm_user");
			$data_user = $model_userName->where('user_id='.$value['user_id'])->find();
			$array_visitlog[$key]['user_name'] = $data_user['user_name'];
		}
		$json_visitlog = json_encode($array_visitlog);
		$this->assign('json_visitlog',$json_visitlog);

		//	商户服务类型下拉框数据
		$model_businesstype = D("bypal_facilitator_itype");
		$array_businesstype = $model_businesstype->select();
		$this->assign('array_businesstype',$array_businesstype);

		//	商户所在商圈下拉框数据
		$model_circle = D("bypal_facilitator_circle");
		$array_circle = $model_circle->where('circle_mark="1"')->select();
		$this->assign('array_circle',$array_circle);

		//	商务代表下拉框数据
		$model_user = D("bypal_comm_user");
		$array_user = $model_user->where('user_depart="0"')->select();
		$this->assign('array_user',$array_user);

		$this->display();
	}


	/**
	 *	商户拜访日志新增
	 */
	public function visitlog_add() 
	{
		//	商户服务类型下拉框数据
		$model_businesstype = D("bypal_facilitator_itype");
		$array_businesstype = $model_businesstype->select();
		$this->assign('array_businesstype',$array_businesstype);

		//	商户所在商圈下拉框数据
		$model_circle = D("bypal_facilitator_circle");
		$array_circle = $model_circle->where('circle_mark="1"')->select();
		$this->assign('array_circle',$array_circle);

		if ($_POST && array_key_exists('visitlog_name', $_POST) && $_POST['visitlog_name']) 
		{
			$select = D('bypal_comm_user');
			$data_select = $select->where('user_id='.session('regist_id'))->find();

			$insert = D('bypal_facilitator_visitlog');
			$data['visitlog_name'] = $_POST['visitlog_name'];
			$data['visitlog_type'] = $_POST['visitlog_type'];
			$data['user_id'] = $data_select['user_id'];
			$data['user_phone'] = $data_select['user_phone'];
			$data['visitlog_address'] = $_POST['visitlog_address'];
			$data['businesstype_id'] = $_POST['businesstype_id'];
			$data['circle_id'] = $_POST['circle_id'];
			$data['visitlog_person'] = $_POST['visitlog_person'];
			$data['visitlog_phone'] = $_POST['visitlog_phone'];
			$data['create_time'] = time();
			$data['update_time'] = time();
			if ($result = $insert->add($data)) 
			{
				$insert_visitlogdetail = D('bypal_facilitator_visitlogdetail');
				$data_visitlogdetail['visitlog_id'] = $result;
				$data_visitlogdetail['visitlogdetail_returntasktrue'] = '首次拜访';
				$data_visitlogdetail['visitlogdetail_person'] = $_POST['visitlog_person'];
				$data_visitlogdetail['visitlogdetail_phone'] = $_POST['visitlog_phone'];
				$data_visitlogdetail['visitlogdetail_result'] = $_POST['visitlogdetail_result'];
				$data_visitlogdetail['visitlogdetail_returndate'] = strtotime($_POST['visitlogdetail_returndate']);
				$data_visitlogdetail['visitlogdetail_returntask'] = $_POST['visitlogdetail_returntask'];
				$data_visitlogdetail['create_time'] = time();
				$data_visitlogdetail['update_time'] = time();
				if ($insert_visitlogdetail->add($data_visitlogdetail)) 
				{
					echo "<script>alert('新增成功！');window.location.href='../../facilitator/visitlog/visitlog_add';</script>";
				}
				else
				{
					echo "<script>alert('新增失败，需要回滚！');history.back();</script>";
				}
			}
			else
			{
				echo "<script>alert('新增失败！');history.back();</script>";
			}
			exit;
		}

		$this->display();
	}


	/**
	 *	商户拜访日志详情
	 */
	public function visitlog_see() 
	{
		if (IS_GET && I('get.visitlog_id')) 
		{
			$model = D("bypal_facilitator_visitlog");
			$data = $model->where('visitlog_id='.I('get.visitlog_id'))->find();

			$shopby_table = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($shopby_table->where('visitlog_id='.$data['visitlog_id'])->find()) 
			{
				$data['shopby_table'] = '1';
			}
			else
			{
				$data['shopby_table'] = '0';
			}

			if ($data['visitlog_type'] == '1') 
			{
				$data['visitlog_type'] = '主动拜访';
			}
			elseif ($data['visitlog_type'] == '2') 
			{
				$data['visitlog_type'] = '商户联系';
			}

			if ($data['visitlog_mark'] == '0') 
			{
				$data['mark'] = '淘汰';
			}
			elseif ($data['visitlog_mark'] == '1') 
			{
				$data['mark'] = '跟进';
			}
			elseif ($data['visitlog_mark'] == '2') 
			{
				$data['mark'] = '已转审核';
			}

			$model_businesstype = D("bypal_facilitator_itype");
			$data_businesstype = $model_businesstype->where('id='.$data['businesstype_id'])->find();
			$data['businesstype_name'] = $data_businesstype['name'];

			$model_circle = D("bypal_facilitator_circle");
			$data_circle = $model_circle->where('circle_id='.$data['circle_id'])->find();
			$data['circle_name'] = $data_circle['circle_name'];

			$model_user = D("bypal_comm_user");
			$data_user = $model_user->where('user_id='.$data['user_id'])->find();
			$data['user_name'] = $data_user['user_name'];

			$model_detail = D("bypal_facilitator_visitlogdetail");
			$count = $model_detail->where('visitlog_id='.$data['visitlog_id'])->count();
			$data['count'] = $count.'次';

			$this->assign('data',$data);

			$array_data = $model_detail->where('visitlog_id='.$data['visitlog_id'])->order('create_time asc')->select();
			foreach ($array_data as $key => $value) 
			{
				$array_data[$key]['key'] = '第'.($key+1).'次';

				$array_data[$key]['create_time'] = date('Y-m-d', $value['create_time']);
				$array_data[$key]['visitlogdetail_returndate'] = date('Y-m-d', $value['visitlogdetail_returndate']);
			}
			$json_data = json_encode($array_data);
			$this->assign('json_data',$json_data);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	商户拜访日志详情#####新增记录or修改记录
	 */
	public function visitlogdetail_add() 
	{
		if ($_POST && array_key_exists('addData', $_POST) && $_POST['addData']) 
		{
			$data = $_POST['addData'][0];

			$model = D("bypal_facilitator_visitlogdetail");

			if ($data['visitlogdetail_id'] == '') 
			{
				//	新增
				$insert['visitlog_id'] = $data['visitlog_id'];
				$insert['visitlogdetail_returntasktrue'] = $data['visitlogdetail_returntasktrue'];
				$insert['visitlogdetail_person'] = $data['visitlogdetail_person'];
				$insert['visitlogdetail_phone'] = $data['visitlogdetail_phone'];
				$insert['visitlogdetail_result'] = $data['visitlogdetail_result'];
				$insert['visitlogdetail_returndate'] = strtotime($data['visitlogdetail_returndate']);
				$insert['visitlogdetail_returntask'] = $data['visitlogdetail_returntask'];
				$insert['create_time'] = time();
				$insert['update_time'] = time();
				$model->add($insert);
			}
			else
			{
				//	修改
				$update['visitlogdetail_returntasktrue'] = $data['visitlogdetail_returntasktrue'];
				$update['visitlogdetail_person'] = $data['visitlogdetail_person'];
				$update['visitlogdetail_phone'] = $data['visitlogdetail_phone'];
				$update['visitlogdetail_result'] = $data['visitlogdetail_result'];
				$update['visitlogdetail_returndate'] = strtotime($data['visitlogdetail_returndate']);
				$update['visitlogdetail_returntask'] = $data['visitlogdetail_returntask'];
				$update['update_time'] = time();
				$model->where('visitlogdetail_id='.$data['visitlogdetail_id'])->save($update);
			}
		}
	}


	/**
	 *	商户拜访日志详情#####商户淘汰or跟进动作
	 */
	public function visitlog_del() 
	{
		if ($_POST && array_key_exists('delData', $_POST) && $_POST['delData']) 
		{
			$data = $_POST['delData'];

			$model = D("bypal_facilitator_visitlog");
			$result = $model->where('visitlog_id='.$data)->find();
			if ($result['visitlog_mark'] == '0') 
			{
				$model->visitlog_mark = '1';
			}
			elseif ($result['visitlog_mark'] == '1') 
			{
				$model->visitlog_mark = '0';
			}
			$model->update_time = time();
			if ($model->where('visitlog_id='.$data)->save()) 
			{
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 *	提交审核资料
	 */
	public function check_submit() 
	{
		if ($_POST && array_key_exists('submit', $_POST) && $_POST['submit']) 
		{
			$data = $_POST['submit'];

			$model = D("bypal_facilitator_visitlog");
			$model->visitlog_mark = '2';
			$model->update_time = time();

			$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			$model_shopby->mark = '2';
			if ($model->where('visitlog_id='.$data)->save() && $model_shopby->where('visitlog_id='.$data)->save()) 
			{
				$result = '1';

				$data_visitlog = $model->where('visitlog_id='.$data)->find();

				$path = "Public/upload";
				if (!is_dir($path)) {mkdir($path,0777);}
				$path = $path . '/visitlog';
				if (!is_dir($path)) {mkdir($path,0777);}
				$file_name = "商户拜访日志详情".$data.".txt";
				$txt = $path.'/'.$file_name;
				$file = fopen($txt,"a+");

				$file_string = str_pad("商户名称：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['visitlog_name'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("拜访方式：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['visitlog_type'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("拜访次数：", 18, " ", STR_PAD_RIGHT).str_pad(($data_visitlog['visitlog_id']."次"), 45, " ", STR_PAD_RIGHT)
					." \r\n"
					.str_pad("商户服务类型：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['businesstype_id'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("商户所属商圈：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['circle_id'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("商户地址：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['visitlog_address'], 45, " ", STR_PAD_RIGHT)
					." \r\n"
					.str_pad("联系人：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['visitlog_person'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("联系电话：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['visitlog_phone'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("创建时间：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['create_time'], 45, " ", STR_PAD_RIGHT)
					." \r\n"
					.str_pad("商务代表：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['user_id'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("商务代表电话：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['user_phone'], 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("更新时间：", 18, " ", STR_PAD_RIGHT).str_pad($data_visitlog['update_time'], 45, " ", STR_PAD_RIGHT)
					." \r\n";


				$file_string .= " \r\n拜访日志明细： \r\n";
				$file_string .= str_pad("拜访次数", 12, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("回访任务", 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("回访时间", 20, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("商户负责人", 20, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("联系电话", 20, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("拜访结果", 45, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("预计回访日期", 20, " ", STR_PAD_RIGHT)
					.str_pad("|", 8, " ", STR_PAD_RIGHT)
					.str_pad("预计回访任务", 45, " ", STR_PAD_RIGHT)
					." \r\n";
				$model_detail = D("bypal_facilitator_visitlogdetail");
				$array_detail = $model_detail->where('visitlog_id='.$data_visitlog['visitlog_id'])->order('create_time desc')->select();
				foreach ($array_detail as $key => $value) 
				{
					$file_string .= str_pad("第".($key+1)."次", 12, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_returntasktrue'], 45, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad(date('Y-m-d', $value['create_time']), 20, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_person'], 20, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_phone'], 20, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_result'], 45, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_returndate'], 20, " ", STR_PAD_RIGHT)
						.str_pad("|", 8, " ", STR_PAD_RIGHT)
						.str_pad($value['visitlogdetail_returntask'], 45, " ", STR_PAD_RIGHT)
						." \r\n";
				}
				
				$file_string = iconv("UTF-8", "gbk//TRANSLIT", $file_string);
				fwrite($file, $file_string." \r\n");

				$model_file = M('bypal_shopby_file');
				$insert['visitlog_id'] = $data;
				$insert['file_address'] = $file_name;
				$model_file->add($insert);
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 *	编辑审核资料
	 */
	public function visitlog_checkinfo() 
	{
		if (IS_GET && I('get.visitlog_id')) 
		{
			$data = I('get.visitlog_id');
			$this->assign('data',$data);

			$model_itype = D("bypal_facilitator_itype");
			$array_itype = $model_itype->select();
			$this->assign('array_itype',$array_itype);

			$model_visitlog = M('bypal_facilitator_visitlog');
			$data_visitlog = $model_visitlog->where('visitlog_id='.$data)->find();

			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			$result = $model->where('visitlog_id='.$data)->find();
			$result['edttime'] = date('Y-m-d', $result['edttime']);
			$result['regtime'] = date('Y-m-d', $result['regtime']);
			if ($result['author'] == '') 
			{
				$model_user = D("bypal_comm_user");
				$data_user = $model_user->where('user_id='.$data_visitlog['user_id'])->find();
				$result['author'] = $data_user['user_name'];
			}
			if ($result['merchant_name'] == '') 
			{
				$result['merchant_name'] = $data_visitlog['visitlog_name'];
			}
			if ($result['merchant_abbreviation'] == '') 
			{
				$result['merchant_abbreviation'] = $data_visitlog['visitlog_name'];
			}
			if ($result['service_type'] == '') 
			{
				$data_itype = $model_itype->where('id='.$data_visitlog['businesstype_id'])->find();
				$result['service_type'] = $data_itype['name'];
			}
			if ($result['merchant_address'] == '') 
			{
				$result['merchant_address'] = $data_visitlog['visitlog_address'];
			}
			if ($result['principal'] == '') 
			{
				$result['principal'] = $data_visitlog['visitlog_person'];
			}
			if ($result['principal_phone'] == '') 
			{
				$result['principal_phone'] = $data_visitlog['visitlog_phone'];
			}
			$this->assign('result',$result);

			$model_files = M('bypal_shopby_file');
			$files = $model_files->where('visitlog_id='.$data.' and file_mark="1"')->select();
			$this->assign('files',$files);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	编辑审核资料#####1.基本资料提交
	 */
	public function baseinfo_submit() 
	{
		if ($_POST && array_key_exists('baseinfo', $_POST) && $_POST['baseinfo']) 
		{
			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($model->where('visitlog_id='.$_POST['baseinfo'])->find()) 
			{
				//	修改
				$update['author'] = $_POST['author'];
				$update['edttime'] = strtotime($_POST['edttime']);
				$update['merchant_name'] = $_POST['merchant_name'];
				$update['merchant_address'] = $_POST['merchant_address'];
				$update['merchant_abbreviation'] = $_POST['merchant_abbreviation'];
				$update['regtime'] = strtotime($_POST['regtime']);
				$update['interest_rate'] = $_POST['interest_rate'];
				$update['service_type'] = $_POST['service_type'];
				$update['corporates'] = $_POST['corporates'];
				$update['corporate_phone'] = $_POST['corporate_phone'];
				$update['corporate_email'] = $_POST['corporate_email'];
				$update['principal'] = $_POST['principal'];
				$update['principal_phone'] = $_POST['principal_phone'];
				$update['principal_email'] = $_POST['principal_email'];
				$update['down_payment'] = $_POST['down_payment'];
				$update['cash_deposit'] = $_POST['cash_deposit'];
				$update['pos_number'] = $_POST['pos_number'];
				$model->where('visitlog_id='.$_POST['baseinfo'])->save($update);
			}
			else
			{
				//	新增
				$insert['visitlog_id'] = $_POST['baseinfo'];
				$insert['author'] = $_POST['author'];
				$insert['edttime'] = strtotime($_POST['edttime']);
				$insert['merchant_name'] = $_POST['merchant_name'];
				$insert['merchant_address'] = $_POST['merchant_address'];
				$insert['merchant_abbreviation'] = $_POST['merchant_abbreviation'];
				$insert['regtime'] = strtotime($_POST['regtime']);
				$insert['interest_rate'] = $_POST['interest_rate'];
				$insert['service_type'] = $_POST['service_type'];
				$insert['corporates'] = $_POST['corporates'];
				$insert['corporate_phone'] = $_POST['corporate_phone'];
				$insert['corporate_email'] = $_POST['corporate_email'];
				$insert['principal'] = $_POST['principal'];
				$insert['principal_phone'] = $_POST['principal_phone'];
				$insert['principal_email'] = $_POST['principal_email'];
				$insert['down_payment'] = $_POST['down_payment'];
				$insert['cash_deposit'] = $_POST['cash_deposit'];
				$insert['pos_number'] = $_POST['pos_number'];
				$insert['ctime'] = time();
				$model->add($insert);
			}

			echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['baseinfo']."';</script>";
			exit;
		}
	}


	/**
	 *	编辑审核资料#####2.办公场地提交
	 */
	public function office_submit() 
	{
		if ($_POST && array_key_exists('office', $_POST) && $_POST['office']) 
		{
			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($model->where('visitlog_id='.$_POST['office'])->find()) 
			{
				//	修改
				$update['floor_space'] = $_POST['floor_space'];
				$update['office_environment'] = $_POST['office_environment'];
				$update['durable_years'] = $_POST['durable_years'];
				$update['surrounding'] = $_POST['surrounding'];
				$update['site_property'] = $_POST['site_property'];
				$update['business_license'] = $_POST['business_license'];
				$model->where('visitlog_id='.$_POST['office'])->save($update);
			}
			else
			{
				//	新增
				$insert['visitlog_id'] = $_POST['office'];
				$insert['floor_space'] = $_POST['floor_space'];
				$insert['office_environment'] = $_POST['office_environment'];
				$insert['durable_years'] = $_POST['durable_years'];
				$insert['surrounding'] = $_POST['surrounding'];
				$insert['site_property'] = $_POST['site_property'];
				$insert['business_license'] = $_POST['business_license'];
				$insert['ctime'] = time();
				$model->add($insert);
			}

			echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['office']."';</script>";
			exit;
		}
	}


	/**
	 *	编辑审核资料#####3.主营业务提交
	 */
	public function business_submit() 
	{
		if ($_POST && array_key_exists('business', $_POST) && $_POST['business']) 
		{
			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($model->where('visitlog_id='.$_POST['business'])->find()) 
			{
				//	修改
				$update['main_project'] = $_POST['main_project'];
				$update['average_monthly_turnover'] = $_POST['average_monthly_turnover'];
				$update['age_limit'] = $_POST['age_limit'];
				$update['seasonality'] = $_POST['seasonality'];
				$update['price'] = $_POST['price'];
				$update['period'] = $_POST['period'];
				$update['student_number'] = $_POST['student_number'];
				$update['max_galleryful'] = $_POST['max_galleryful'];
				$update['assigning_job'] = $_POST['assigning_job'];
				$update['reputation'] = $_POST['reputation'];
				$update['staff_number'] = $_POST['staff_number'];
				$update['work_state'] = $_POST['work_state'];
				$model->where('visitlog_id='.$_POST['business'])->save($update);
			}
			else
			{
				//	新增
				$insert['visitlog_id'] = $_POST['business'];
				$insert['main_project'] = $_POST['main_project'];
				$insert['average_monthly_turnover'] = $_POST['average_monthly_turnover'];
				$insert['age_limit'] = $_POST['age_limit'];
				$insert['seasonality'] = $_POST['seasonality'];
				$insert['price'] = $_POST['price'];
				$insert['period'] = $_POST['period'];
				$insert['student_number'] = $_POST['student_number'];
				$insert['max_galleryful'] = $_POST['max_galleryful'];
				$insert['assigning_job'] = $_POST['assigning_job'];
				$insert['reputation'] = $_POST['reputation'];
				$insert['staff_number'] = $_POST['staff_number'];
				$insert['work_state'] = $_POST['work_state'];
				$insert['ctime'] = time();
				$model->add($insert);
			}

			echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['business']."';</script>";
			exit;
		}
	}


	/**
	 *	编辑审核资料#####4.支出估计提交
	 */
	public function expenses_submit() 
	{
		if ($_POST && array_key_exists('expenses', $_POST) && $_POST['expenses']) 
		{
			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($model->where('visitlog_id='.$_POST['expenses'])->find()) 
			{
				//	修改
				$update['staff_wages'] = $_POST['staff_wages'];
				$update['utility_bills'] = $_POST['utility_bills'];
				$update['liabilities'] = $_POST['liabilities'];
				$update['rests_cost'] = $_POST['rests_cost'];
				$model->where('visitlog_id='.$_POST['expenses'])->save($update);
			}
			else
			{
				//	新增
				$insert['visitlog_id'] = $_POST['expenses'];
				$insert['staff_wages'] = $_POST['staff_wages'];
				$insert['utility_bills'] = $_POST['utility_bills'];
				$insert['liabilities'] = $_POST['liabilities'];
				$insert['rests_cost'] = $_POST['rests_cost'];
				$insert['ctime'] = time();
				$model->add($insert);
			}

			echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['expenses']."';</script>";
			exit;
		}
	}


	/**
	 *	编辑审核资料#####5.注意事项提交 & 文件上传提交
	 */
	public function notice_submit() 
	{
		if ($_POST && array_key_exists('notice', $_POST) && $_POST['notice']) 
		{
			$model = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($model->where('visitlog_id='.$_POST['notice'])->find()) 
			{
				//	修改
				$update['shops'] = $_POST['shops'];
				$model->where('visitlog_id='.$_POST['notice'])->save($update);
			}
			else
			{
				//	新增
				$insert['visitlog_id'] = $_POST['notice'];
				$insert['shops'] = $_POST['shops'];
				$insert['ctime'] = time();
				$model->add($insert);
			}

			if ($_POST['files']) 
			{
				if ($_FILES['files']) 
				{
					$files = $this->uploaddata($_FILES['files']);
					if($files == 2 || $files == 3) 
					{
						$files = $_POST['files'];
					}

					$model_file = M('bypal_shopby_file');
					$insert_file['visitlog_id'] = $_POST['notice'];
					$insert_file['file_address'] = $files;
					$model_file->add($insert_file);
				}
			}

			echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['notice']."';</script>";
			exit;
		}
	}


	// /**
	//  *	编辑审核资料#####6.文件上传提交
	//  */
	// public function file_submit() 
	// {
	// 	if ($_POST && array_key_exists('file', $_POST) && $_POST['file']) 
	// 	{
	// 		$files = '';
	// 		if ($_FILES['files']) 
	// 		{
	// 			$files = $this->uploaddata($_FILES['files']);
	// 			if($files == 2 || $files == 3) 
	// 			{
	// 				$files = $_POST['files'];
	// 			}
	// 		}

	// 		$model = M('bypal_shopby_file');
	// 		$insert['visitlog_id'] = $_POST['file'];
	// 		$insert['file_address'] = $files;
	// 		$model->add($insert);

	// 		echo "<script>window.location.href='../../facilitator/visitlog/visitlog_checkinfo?visitlog_id=".$_POST['file']."';</script>";
	// 		exit;
	// 	}
	// }


	//	文件上传
	public function uploaddata($onefile) 
	{
		if ($onefile['error'] === 0) 
		{
			$path = "Public/upload";
			if (!is_dir($path)) {mkdir($path,0777);}
			$path = $path . '/visitlog';
			if (!is_dir($path)) {mkdir($path,0777);}

			if($onefile ["size"] < 3000000) 
			{
				$file1 = time().rand(10, 99).strrchr($onefile["name"],".");
				$file2 = $path.'/'.$file1;
				if(move_uploaded_file($onefile["tmp_name"],$file2)) 
				{
					return $file1;
				}
				else
				{
					return 3;
				}
			}
			else
			{
				return 2;
			}
		}
		else
		{
			return 3;
		}
	}


	/**
	 *	编辑审核资料#####文件删除动作
	 */
	public function del_file() 
	{
		if ($_POST && array_key_exists('delData', $_POST) && $_POST['delData']) 
		{
			$data = $_POST['delData'];

			$model = D("bypal_shopby_file");
			$model->file_mark = '0';
			if ($model->where('file_id='.$data)->save()) 
			{
				$data_file = $model->where('file_id='.$data)->find();
				unlink("Public/upload/visitlog/".$data_file['file_address']);
				// $model->where('file_id='.$data)->delete();
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}

}